package m.jclecai.com.utils;

import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import m.jclecai.com.bean.Content;
import m.jclecai.com.bean.JclqDXFItme;
import m.jclecai.com.bean.JclqItme;
import m.jclecai.com.bean.JclqRQSFItme;
import m.jclecai.com.bean.JclqSFItme;
import m.jclecai.com.bean.TrendDataBean;
import m.jclecai.com.bean.ZqBQCItem;
import m.jclecai.com.bean.ZqCBFItem;
import m.jclecai.com.bean.ZqHHItem;
import m.jclecai.com.bean.ZqItem;
import m.jclecai.com.bean.ZqJQSItem;
import m.jclecai.com.bean.ZqSPFItem;

/**
 * Created by hp on 2017/4/13.
 */

public class StringUtils {
    /**
     * 拼接字符串，通过该字符串算奖金和注数
     * 竞彩足球里面的胜平负
     *
     * @return
     */
    public static String readBounsCode(List<ZqSPFItem> items) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer();
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqSPFItem item = items.get(i);
            ArrayList<String> list = item.getOdd_select();
            ArrayList<String> rlist = item.getOdd_select_r();
            ArrayList<String> odd_sp = item.getOdd_sp();
            ArrayList<String> odd_rsp = item.getOdd_rsp();
            if (list.size() > 0 || rlist.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getCodes()[0] + ">");
                } else {
                    codeBuff.append("," + item.getCodes()[0] + ">");
                }
                arr[i] = list.size() + rlist.size();
                if (rlist.size() > 0) {//有让球胜平负的同时也有胜平负
                    codeBuff.append("RSPF=");
                    for (int j = 0; j < rlist.size(); j++) {
                        codeBuff.append(rlist.get(j) + "/");

                    }
                    codeBuff.deleteCharAt(codeBuff.length() - 1);
                    if (odd_rsp != null && odd_rsp.size() != 0) {
                        codeBuff.append("(");
                        for (int f = 0; f < odd_rsp.size(); f++) {
                            codeBuff.append(odd_rsp.get(f) + "~");
                        }
                        codeBuff.deleteCharAt(codeBuff.length() - 1);
                        codeBuff.append(")");
                    }

                    if (list.size() > 0) {
                        codeBuff.append("+SPF=");
                        for (int j = 0; j < list.size(); j++) {
                            codeBuff.append(list.get(j) + "/");
                        }
                        codeBuff.deleteCharAt(codeBuff.length() - 1);
                        if (odd_sp != null && odd_sp.size() != 0) {
                            codeBuff.append("(");
                            for (int f = 0; f < odd_sp.size(); f++) {
                                codeBuff.append(odd_sp.get(f) + "~");
                            }
                            codeBuff.deleteCharAt(codeBuff.length() - 1);
                            codeBuff.append(")");
                        }
                    }
                } else {//只有胜平负
                    codeBuff.append("SPF=");
                    for (int j = 0; j < list.size(); j++) {
                        codeBuff.append(list.get(j) + "/");
                    }
                    codeBuff.deleteCharAt(codeBuff.length() - 1);
                    //拼后面一部分
                    if (odd_sp != null && odd_sp.size() != 0) {
                        codeBuff.append("(");
                        for (int f = 0; f < odd_sp.size(); f++) {
                            codeBuff.append(odd_sp.get(f) + "~");
                        }
                        codeBuff.deleteCharAt(codeBuff.length() - 1);
                        codeBuff.append(")");
                    }
                }
            }
        }
        //170410001>SPF=3/1/0,170410002>SPF=3,170410003>SPF=3
        String code = codeBuff.toString();
        return code;
    }

    /**
     * 竞彩足球胜平负投注时的购彩码
     */
    public static String readSPFCode(List<ZqSPFItem> items, String value) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer("HH|");
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqSPFItem item = items.get(i);
            ArrayList<String> list = item.getOdd_select();
            ArrayList<String> rlist = item.getOdd_select_r();
            if (list.size() > 0 || rlist.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getCodes()[0] + ">");
                } else {
                    codeBuff.append("," + item.getCodes()[0] + ">");
                }
                arr[i] = list.size() + rlist.size();
                if (rlist.size() > 0) {
                    codeBuff.append("RSPF=");
                    for (int j = 0; j < rlist.size(); j++) {
                        codeBuff.append(rlist.get(j) + "/");
                    }
                    if (list.size() > 0) {
                        codeBuff.deleteCharAt(codeBuff.length() - 1);
                        codeBuff.append("+SPF=");
                        for (int j = 0; j < list.size(); j++) {
                            codeBuff.append(list.get(j) + "/");
                        }
                    }
                } else {
                    codeBuff.append("SPF=");
                    for (int j = 0; j < list.size(); j++) {
                        codeBuff.append(list.get(j) + "/");
                    }
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);
            }
        }
        codeBuff.append("|" + value);
        String code = codeBuff.toString();

        return code;
    }

    /**
     * 拼接字符串，通过该字符串算奖金和注数
     * 竞彩足球里面的胜平负
     *
     * @return
     */
    public static String readJCZQHHBounsCode(List<ZqHHItem> items) {
        StringBuffer codeBuff = new StringBuffer();
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqHHItem item = items.get(i);
            HashMap<String, String> fmap = item.getOdd_select();
            HashMap<String, String> rmap = item.getOdd_select_r();
            HashMap<String, String> bmap = item.getOdd_select_bqc();
            HashMap<String, String> jmap = item.getOdd_select_jqs();
            HashSet<String> set = item.getOdd_select_cbf();

            ArrayList<String> odd_sp = item.getOdd_sp();
            ArrayList<String> odd_rsp = item.getOdd_rsp();
            if (i < 1) {
                codeBuff.append(item.getCodes()[0] + ">");
            } else {
                codeBuff.append("," + item.getCodes()[0] + ">");
            }
            if (fmap.size() > 0) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("SPF=");
                Iterator<Entry<String, String>> ltera = fmap.entrySet().iterator();
                while (ltera.hasNext()) {
                    Entry<String, String> entry = (Entry<String, String>) ltera.next();
                    buffer.append(entry.getKey() + "/");
                }
                buffer.deleteCharAt(buffer.length() - 1);
                //拼后面一部分
                if (odd_sp != null && odd_sp.size() != 0) {
                    buffer.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        buffer.append(odd_sp.get(f) + "~");
                    }
                    buffer.deleteCharAt(buffer.length() - 1);
                    buffer.append(")");
                }
                codeBuff.append(buffer.toString() + "+");
            }

            if (rmap.size() > 0) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("RSPF=");
                Iterator<Entry<String, String>> ltera = rmap.entrySet().iterator();
                while (ltera.hasNext()) {
                    Entry<String, String> entry = (Entry<String, String>) ltera.next();
                    buffer.append(entry.getKey() + "/");
                }
                buffer.deleteCharAt(buffer.length() - 1);
                //拼后面一部分
                if (odd_sp != null && odd_sp.size() != 0) {
                    buffer.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        buffer.append(odd_sp.get(f) + "~");
                    }
                    buffer.deleteCharAt(buffer.length() - 1);
                    buffer.append(")");
                }
                codeBuff.append(buffer.toString() + "+");
            }

            if (bmap.size() > 0) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("BQC=");
                Iterator<Entry<String, String>> ltera = bmap.entrySet().iterator();
                while (ltera.hasNext()) {
                    Entry<String, String> entry = (Entry<String, String>) ltera.next();
                    buffer.append(entry.getKey() + "/");
                }
                buffer.deleteCharAt(buffer.length() - 1);
                codeBuff.append(buffer.toString() + "+");
            }

            if (jmap.size() > 0) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("JQS=");
                Iterator<Entry<String, String>> ltera = jmap.entrySet().iterator();
                while (ltera.hasNext()) {
                    Entry<String, String> entry = (Entry<String, String>) ltera.next();
                    buffer.append(entry.getKey() + "/");
                }
                buffer.deleteCharAt(buffer.length() - 1);
                codeBuff.append(buffer.toString() + "+");
            }

            if (set.size() > 0) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("CBF=");
                Iterator<String> ltera = set.iterator();
                while (ltera.hasNext()) {
                    buffer.append(ltera.next() + "/");
                }
                buffer.deleteCharAt(buffer.length() - 1);
                codeBuff.append(buffer.toString() + "+");
            }
            codeBuff.deleteCharAt(codeBuff.length() - 1);
        }
        String code = codeBuff.toString();
//HH|170414001>SPF=3,170414002>SPF=3
        //   return items.size();
        return code;
    }

    /**
     * 竞彩足球的比分
     */
    public static String readJCZQBFBounsCode(List<ZqCBFItem> items) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer();
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqCBFItem item = items.get(i);
            LinkedList<String> list = item.getBfStrings();
            ArrayList<String> odd_sp = item.getOdd_sp();
            if (list.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getStrings()[0] + ">BF=");
                } else {
                    codeBuff.append("," + item.getStrings()[0] + ">BF=");
                }
                arr[i] = list.size();
                for (int j = 0; j < list.size(); j++) {
                    codeBuff.append(list.get(j) + "/");
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);
                if (odd_sp != null && odd_sp.size() != 0) {
                    codeBuff.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        codeBuff.append(odd_sp.get(f) + "~");
                    }
                    codeBuff.deleteCharAt(codeBuff.length() - 1);
                    codeBuff.append(")");
                }
            }
        }
        //170417001=2:0/2:2/0:5,170417002=2:2/2:5
        //170418001>BF=2:1/2:2/1:2(30.00~20.00~8.25),170418002>BF=2:1/2:2/3:3/0:3(12.00~14.00~70.00~14.00)
        String code = codeBuff.toString();
        return code;
    }

    /**
     * 竞彩足球比分的投注购彩码
     *
     * @return
     */
    public static String readBFCode(List<ZqCBFItem> items, String value) {

        int len = 0;
        StringBuffer codeBuff = new StringBuffer("CBF|");
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqCBFItem item = items.get(i);
            LinkedList<String> list = item.getBfStrings();
            if (list.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getStrings()[0] + "=");
                } else {
                    codeBuff.append("," + item.getStrings()[0] + "=");
                }
                arr[i] = list.size();
                for (int j = 0; j < list.size(); j++) {
                    codeBuff.append(list.get(j) + "/");
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);
            }
        }
        codeBuff.append("|" + value);
        String code = codeBuff.toString();
        return code;
    }

    /**
     * 竞彩足球的总进球
     */
    public static String readJCZQZJQBounsCode(List<ZqJQSItem> items) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer();
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqJQSItem item = items.get(i);
            LinkedList<String> list = item.getList();
            ArrayList<String> odd_sp = item.getOdd_sp();
            if (list.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getStrings()[0] + ">ZJQ=");
                } else {
                    codeBuff.append("," + item.getStrings()[0] + ">ZJQ=");
                }
                arr[i] = list.size();
                for (int j = 0; j < list.size(); j++) {
                    codeBuff.append(list.get(j) + "/");
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);

                if (odd_sp != null && odd_sp.size() != 0) {
                    codeBuff.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        codeBuff.append(odd_sp.get(f) + "~");
                    }
                    codeBuff.deleteCharAt(codeBuff.length() - 1);
                    codeBuff.append(")");
                }
            }
        }
        //  170418001=0/5/1/6/2(19.00~6.00~7.60~8.50~4.30),170418002=1/6/7/2(3.80~23.00~34.00~3.05)
        String code = codeBuff.toString();
        return code;
    }

    public static String readZJQCode(List<ZqJQSItem> items, String value) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer("JQS|");
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqJQSItem item = items.get(i);
            LinkedList<String> list = item.getList();
            if (list.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getStrings()[0] + "=");
                } else {
                    codeBuff.append("," + item.getStrings()[0] + "=");
                }
                arr[i] = list.size();
                for (int j = 0; j < list.size(); j++) {
                    codeBuff.append(list.get(j) + "/");
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);
            }
        }
        codeBuff.append("|" + value);
        String code = codeBuff.toString();
        return code;
    }

    /**
     * 竞彩足球的半全场
     */
    public static String readJCZQBQCBounsCode(List<ZqBQCItem> items) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer();
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqBQCItem item = items.get(i);
            LinkedList<String> list = item.getCodeList();
            ArrayList<String> odd_sp = item.getOdd_sp();
            if (list.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getCodes()[0] + ">BQC=");
                } else {
                    codeBuff.append("," + item.getCodes()[0] + ">BQC=");
                }
                arr[i] = list.size();
                for (int j = 0; j < list.size(); j++) {
                    codeBuff.append(list.get(j) + "/");
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);

                if (odd_sp != null && odd_sp.size() != 0) {
                    codeBuff.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        codeBuff.append(odd_sp.get(f) + "~");
                    }
                    codeBuff.deleteCharAt(codeBuff.length() - 1);
                    codeBuff.append(")");
                }
            }
        }
        String code = codeBuff.toString();
        return code;
    }

    /**
     * 竞彩足球半全场投注时的购彩码
     */
    public static String readBQCCode(List<ZqBQCItem> items, String value) {
        int len = 0;
        StringBuffer codeBuff = new StringBuffer("BQC|");
        int[] arr = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            ZqBQCItem item = items.get(i);
            LinkedList<String> list = item.getList();
            if (list.size() > 0) {
                len++;
                if (i < 1) {
                    codeBuff.append(item.getCodes()[0] + "=");
                } else {
                    codeBuff.append("," + item.getCodes()[0] + "=");
                }
                arr[i] = list.size();
                for (int j = 0; j < list.size(); j++) {
                    codeBuff.append(list.get(j) + "/");
                }
                codeBuff.deleteCharAt(codeBuff.length() - 1);
            }
        }
        codeBuff.append("|" + value);
        String code = codeBuff.toString();
        return code;
    }

    /**
     * 竞彩篮球的胜负
     */
    public static String readJCLQSFBounsCode(List<JclqSFItme> objs) {
        int length = 0;
        StringBuffer zjqsBuffer = new StringBuffer();
        int[] arr = new int[objs.size()];
        for (int i = 0; i < objs.size(); i++) {
            JclqSFItme obj = objs.get(i);
            LinkedList<String> list = obj.getLists();
            ArrayList<String> odd_sp = obj.getOdd_sp();
            if (list.size() > 0) {
                length = length + 1;
                if (i < 1) {
                    zjqsBuffer
                            .append(obj.getStrings()[0]
                                    + ">SF=");
                } else {
                    zjqsBuffer
                            .append(","
                                    + obj.getStrings()[0]
                                    + ">SF=");
                }

                int lists1ize = obj.getLists().size();
                arr[i] = lists1ize;
                for (int j = 0; j < lists1ize; j++) {
                    zjqsBuffer
                            .append(list.get(j) + "/");
                }
                zjqsBuffer.deleteCharAt(zjqsBuffer
                        .length() - 1);
                if (odd_sp != null && odd_sp.size() != 0) {
                    zjqsBuffer.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        zjqsBuffer.append(odd_sp.get(f) + "~");
                    }
                    zjqsBuffer.deleteCharAt(zjqsBuffer.length() - 1);
                    zjqsBuffer.append(")");
                }
            }
        }
//        zjqsBuffer.append("|");
        //170419305>SF=0/3(1.10~4.20),170419301>SF=0/3(1.23~3.05),170419302>SF=0(1.05)
        String codes = zjqsBuffer.toString();
        return codes;
    }

    /**
     * 竞彩篮球胜负的投注页面
     */
    public static String readJCLQSFCode(List<JclqSFItme> objs, String value) {
        int length1 = 0;
        StringBuffer zjqsBuffer = new StringBuffer(
                "SF|");
        int[] arr = new int[objs.size()];
        for (int i = 0; i < objs.size(); i++) {
            JclqSFItme obj = objs.get(i);
            LinkedList<String> list = obj.getLists();
            if (list.size() > 0) {
                length1 = length1 + 1;
                if (i < 1) {
                    zjqsBuffer
                            .append(obj.getStrings()[0]
                                    + "=");
                } else {
                    zjqsBuffer
                            .append(","
                                    + obj.getStrings()[0]
                                    + "=");
                }

                int lists1ize = obj.getLists().size();
                arr[i] = lists1ize;
                for (int j = 0; j < lists1ize; j++) {
                    zjqsBuffer
                            .append(list.get(j) + "/");
                }
                zjqsBuffer.deleteCharAt(zjqsBuffer
                        .length() - 1);
            }
        }
        zjqsBuffer.append("|" + value);
        String codes = zjqsBuffer.toString();
        return codes;
    }

    /**
     * 竞彩篮球里面的让分胜负计算奖金范围
     */
    public static String getreadJCLQRQSFBounsCode(List<JclqRQSFItme> objs) {
        int length1 = 0;
        StringBuffer zjqsBuffer = new StringBuffer();
        int[] arr = new int[objs.size()];
        for (int i = 0; i < objs.size(); i++) {
            JclqRQSFItme obj = objs.get(i);
            LinkedList<String> list = obj.getLists();
            ArrayList<String> odd_sp = obj.getOdd_sp();
            if (list.size() > 0) {
                length1 = length1 + 1;
                if (i < 1) {
                    zjqsBuffer
                            .append(obj.getStrings()[0]
                                    + ">RFSF=");
                } else {
                    zjqsBuffer
                            .append(","
                                    + obj.getStrings()[0]
                                    + ">RFSF=");
                }

                int lists1ize = obj.getLists().size();
                arr[i] = lists1ize;
                for (int j = 0; j < lists1ize; j++) {
                    zjqsBuffer
                            .append(list.get(j) + "/");
                }
                zjqsBuffer.deleteCharAt(zjqsBuffer
                        .length() - 1);

                if (odd_sp != null && odd_sp.size() != 0) {
                    zjqsBuffer.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        zjqsBuffer.append(odd_sp.get(f) + "~");
                    }
                    zjqsBuffer.deleteCharAt(zjqsBuffer.length() - 1);
                    zjqsBuffer.append(")");
                }
            }
        }
        //170420301=0(1.75),170420305=0(1.69),170420302=3(1.75),170420303=0/3(1.75~1.75)
        String codes = zjqsBuffer.toString();
        return codes;
    }

    /**
     * #FF0026
     * 竞彩篮球里面的让球胜负投注时的购彩码
     */
    public static String readJCLQRQSFCode(List<JclqRQSFItme> objs, String value) {
        int length1 = 0;
        StringBuffer zjqsBuffer = new StringBuffer(
                "RFSF|");
        int[] arr = new int[objs.size()];
        for (int i = 0; i < objs.size(); i++) {
            JclqRQSFItme obj = objs.get(i);
            LinkedList<String> list = obj.getLists();
            if (list.size() > 0) {
                length1 = length1 + 1;
                if (i < 1) {
                    zjqsBuffer
                            .append(obj.getStrings()[0]
                                    + "=");
                } else {
                    zjqsBuffer
                            .append(","
                                    + obj.getStrings()[0]
                                    + "=");
                }

                int lists1ize = obj.getLists().size();
                arr[i] = lists1ize;
                for (int j = 0; j < lists1ize; j++) {
                    zjqsBuffer
                            .append(list.get(j) + "/");
                }
                zjqsBuffer.deleteCharAt(zjqsBuffer
                        .length() - 1);
            }
        }
        zjqsBuffer.append("|" + value);
        String codes = zjqsBuffer.toString();
        return codes;
    }


    public static String getJCLQHHBounsCode(List<JclqItme> objs) {
        //计算奖金格式： => codes = 170329001>SPF=3/1(3.2~2.95)+RQSPF=1(3.2),170329002>SPF=0(3.2)
        //170503301>+DXF=3/0(1.75~1.75),170504302>RFSF=0/3(1.82~1.68)+DXF=3/0(1.75~1.75)
        StringBuilder code = new StringBuilder();
        StringBuilder rf = new StringBuilder();
        StringBuilder dx = new StringBuilder();
        Float[] bs = null;
        for (int i = 0; i < objs.size(); i++) {
            JclqItme item = objs.get(i);
            code.append(item.getStrings()[0] + ">");

            StringBuilder rfbs = new StringBuilder();
            StringBuilder dxbs = new StringBuilder();
            if (item.getListMap() != null && item.getListMap().size() != 0) {
                rfbs.append("(");
                dxbs.append("(");
                for (Map.Entry<Integer, String> map : item.getOddSpMap().entrySet()) {
                    if (map.getKey() <= 1) {
                        rfbs.append(map.getValue() + "~");
                    } else {
                        dxbs.append(map.getValue() + "~");
                    }
                }
                rfbs.deleteCharAt(rfbs.length() - 1);
                if (!TextUtils.isEmpty(rfbs))
                    rfbs.append(")");
                dxbs.deleteCharAt(dxbs.length() - 1);
                if (!TextUtils.isEmpty(dxbs))
                    dxbs.append(")");
            }

            for (Map.Entry<Integer, String> map : item.getListMap().entrySet()) {
                if (map.getKey() <= 1) {//RFSF
                    if (TextUtils.isEmpty(rf)) {
                        rf.append("RFSF=" + map.getValue());
                    } else {
                        rf.append("/" + map.getValue());
                    }
                } else {//DXF
                    if (TextUtils.isEmpty(dx)) {
                        dx.append("+DXF=" + map.getValue());
                    } else {
                        dx.append("/" + map.getValue());
                    }
                }
            }
            if (TextUtils.isEmpty(rf))
                dx.delete(0, 1);
            rf.append(rfbs);
            dx.append(dxbs);

            code.append(rf);
            code.append(dx);

            if (i < objs.size() - 1)
                code.append(",");
            dx.delete(0, dx.length());
            rf.delete(0, rf.length());
            bs = null;
        }
        Log.e("getJCLQHHBounsCode", code.toString());
        return code.toString();
    }

    public static String getJCLQHHCode(List<JclqItme> objs, String value) {
        //	 投注格式：codes ： HH|170329001>SPF=3/1+RQSPF=1,170329002>SPF=0|2*1
        //HH|170503301>RFSF=0/3+DXF=3/0,170504302>RFSF=0/3+DXF=3/0|2*1
        String code = "HH|";
        StringBuilder code1 = new StringBuilder();
        StringBuilder rf = new StringBuilder();
        StringBuilder dx = new StringBuilder();
        for (int i = 0; i < objs.size(); i++) {
            JclqItme item = objs.get(i);
            code1.append(item.getStrings()[0] + ">");
            for (Map.Entry<Integer, String> map : item.getListMap().entrySet()) {
                if (map.getKey() <= 1) {//RFSF
                    if (TextUtils.isEmpty(rf)) {
                        rf.append("RFSF=" + map.getValue());
                    } else {
                        rf.append("/" + map.getValue());
                    }
                } else {//DXF
                    if (TextUtils.isEmpty(dx)) {
                        dx.append("+DXF=" + map.getValue());
                    } else {
                        dx.append("/" + map.getValue());
                    }
                }
            }
            if (TextUtils.isEmpty(rf))
                dx.delete(0, 1);
            code1.append(rf);
            code1.append(dx);
            if (i < objs.size() - 1)
                code1.append(",");
            dx.delete(0, dx.length());
            rf.delete(0, rf.length());
        }
        code += code1 + "|" + value;
        Log.e("getJCLQHHCode", code);
        return code;
    }

    /**
     * 竞彩篮球里面的大小分计算奖金范围
     */
    public static String getreadJCLQDXFBounsCode(List<JclqDXFItme> objs) {
        int length1 = 0;
        // DXF|140523301=3,140523302=0|2*1
        StringBuffer zjqsBuffer = new StringBuffer();
        int[] arr = new int[objs.size()];
        for (int i = 0; i < objs.size(); i++) {
            JclqDXFItme obj = objs.get(i);
            LinkedList<String> list = obj.getLists();
            ArrayList<String> odd_sp = obj.getOdd_sp();
            if (list.size() > 0) {
                length1 = length1 + 1;
                if (i < 1) {
                    zjqsBuffer
                            .append(obj.getStrings()[0]
                                    + ">DXF=");
                } else {
                    zjqsBuffer
                            .append(","
                                    + obj.getStrings()[0]
                                    + ">DXF=");
                }

                int lists1ize = obj.getLists().size();
                arr[i] = lists1ize;
                for (int j = 0; j < lists1ize; j++) {
                    zjqsBuffer
                            .append(list.get(j) + "/");
                }
                zjqsBuffer.deleteCharAt(zjqsBuffer
                        .length() - 1);
                if (odd_sp != null && odd_sp.size() != 0) {
                    zjqsBuffer.append("(");
                    for (int f = 0; f < odd_sp.size(); f++) {
                        zjqsBuffer.append(odd_sp.get(f) + "~");
                    }
                    zjqsBuffer.deleteCharAt(zjqsBuffer.length() - 1);
                    zjqsBuffer.append(")");
                }
            }
        }
        String codes = zjqsBuffer.toString();
        return codes;
    }

    /**
     * 竞彩篮球里面大小分的投注时的购彩码
     */
    public static String readJCLQDXFCode(List<JclqDXFItme> objs, String value) {
        int length1 = 0;
        // DXF|140523301=3,140523302=0|2*1
        StringBuffer zjqsBuffer = new StringBuffer(
                "DXF|");
        int[] arr = new int[objs.size()];
        for (int i = 0; i < objs.size(); i++) {
            JclqDXFItme obj = objs.get(i);
            LinkedList<String> list = obj.getLists();
            if (list.size() > 0) {
                length1 = length1 + 1;
                if (i < 1) {
                    zjqsBuffer
                            .append(obj.getStrings()[0]
                                    + "=");
                } else {
                    zjqsBuffer
                            .append(","
                                    + obj.getStrings()[0]
                                    + "=");
                }

                int lists1ize = obj.getLists().size();
                arr[i] = lists1ize;
                for (int j = 0; j < lists1ize; j++) {
                    zjqsBuffer
                            .append(list.get(j) + "/");
                }
                zjqsBuffer.deleteCharAt(zjqsBuffer
                        .length() - 1);
            }
        }
        zjqsBuffer.append("|" + value);
        String codes = zjqsBuffer.toString();
        return codes;
    }


    /**
     * 返回的如：2*1
     * 竞彩足球里面的胜平负
     *
     * @return
     */
    public static String getValue(Map<String, String> map) {
        StringBuilder buffer = new StringBuilder();
        if (map.containsKey("1串1")) {
            buffer.append(map.get("1串1") + ",");
        }
        if (map.containsKey("2串1")) {
            buffer.append(map.get("2串1") + ",");
        }
        if (map.containsKey("3串1")) {
            buffer.append(map.get("3串1") + ",");
        }
        if (map.containsKey("4串1")) {
            buffer.append(map.get("4串1") + ",");
        }
        if (map.containsKey("5串1")) {
            buffer.append(map.get("5串1") + ",");
        }
        if (map.containsKey("6串1")) {
            buffer.append(map.get("6串1") + ",");
        }
        if (map.containsKey("7串1")) {
            buffer.append(map.get("7串1") + ",");
        }
        if (map.containsKey("8串1")) {
            buffer.append(map.get("8串1") + ",");
        }
        if (map.size() > 0) {
            buffer.deleteCharAt(buffer.length() - 1);
        }
        String s = buffer.toString();
        return buffer.toString();
    }


    /**
     * 选择的串数
     * 返回的如：2串1
     * 竞彩足球里面的胜平负
     *
     * @return
     */
    public static String getKeyValue(Map<String, String> map) {
        StringBuilder buffer = new StringBuilder();
        StringBuilder sBuffer1 = new StringBuilder();
        if (map.containsKey("2串1")) {
            buffer.append("2串1,");
            sBuffer1.append("2,");
        }
        if (map.containsKey("3串1")) {
            buffer.append("3串1,");
            sBuffer1.append("3,");
        }
        if (map.containsKey("4串1")) {
            buffer.append("4串1,");
            sBuffer1.append("4,");
        }
        if (map.containsKey("5串1")) {
            buffer.append("5串1,");
            sBuffer1.append("5,");
        }
        if (map.containsKey("6串1")) {
            buffer.append("6串1,");
            sBuffer1.append("6,");
        }
        if (map.containsKey("7串1")) {
            buffer.append("7串1,");
            sBuffer1.append("7,");
        }
        if (map.containsKey("8串1")) {
            buffer.append("8串1,");
            sBuffer1.append("8,");
        }
        if (map.size() > 0) {
            buffer.deleteCharAt(buffer.length() - 1);
            sBuffer1.deleteCharAt(sBuffer1.length() - 1);
        }
        String s = buffer.toString();
        return buffer.toString();
    }


    public static String addUrlParams(String url, String key, String value) {
        String dst = url;
        if (!dst.contains(key)) {
            if (dst.contains("?")) {
                dst += "&";
            } else {
                dst += "?";
            }
            dst += key;
            dst += "=";

            if (!TextUtils.isEmpty(value))
                dst += URLEncoder.encode(value);
        }
        return dst;
    }


    /**
     * 匹配所有
     *
     * @param color
     * @param text
     * @param keyword
     * @return
     */
    public static SpannableString matcherSearchTitle(int color, String text, String keyword) {
        String string = text.toLowerCase();
        String key = keyword.toLowerCase();
        Pattern pattern = Pattern.compile(key);
        Matcher matcher = pattern.matcher(string);
        SpannableString ss = new SpannableString(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    /**
     * 匹配第一个符合的文本
     *
     * @param color
     * @param string
     * @param keyWord
     * @return
     */
    public static CharSequence matcherSearchText(int color, String string, String keyWord) {
        if (TextUtils.isEmpty(keyWord)) return string;
        SpannableStringBuilder builder = new SpannableStringBuilder(string);
        int indexOf = string.indexOf(keyWord);
        if (indexOf != -1) {
            builder.setSpan(new ForegroundColorSpan(color), indexOf, indexOf + keyWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }


    /**
     * 过滤小于选择长度(itemSize)的过关方式特定字符 只限定于彩票过关玩法
     * 2*1 2*2 3*1 3*2 4*1 如果场次只有两场 则最终结果 return 2*1 2*2
     *
     * @param strs
     */
    public static String[] filterData(String[] strs, String perfix, int maxSize) {
        return filterData(strs, perfix, maxSize, 0, false);
    }

    /**
     * 过滤小于选择长度(itemSize)的过关方式特定字符 只限定于彩票过关玩法
     * 2*1 2*2 3*1 3*2 4*1 如果场次只有两场 则最终结果 return 2*1 2*2
     *
     * @param strs
     */
    public static String[] filterData(String[] strs, String perfix, int maxSize, int minSize, boolean isDG) {
        List<String> arr = new ArrayList<>();
        try {
            for (int i = 0; i < strs.length; i++) {
                //如果有2场以上的比赛则过滤单关
                if (maxSize > 1 && i == 0 && !isDG) {
                    continue;
                }
                int num = 0;
                if ("单关".equals(strs[i]))
                    num = 1;
                else
                    num = Integer.parseInt(strs[i].split(perfix)[0]);
                if (minSize < strs.length && num > minSize)
                    if (num <= maxSize) {
                        arr.add(strs[i]);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arr.toArray(new String[]{});
    }

    public static String getBonus(List<ZqItem> zqItemList, String buyType, boolean isHunTou) {
        if (isHunTou)
            return getBonusHH(zqItemList, buyType, false);
        else
            return getBonusWithOutHH(zqItemList, buyType);
    }

    public static String getBonusAward(List<ZqItem> zqItemList, String buyType, boolean isHunTou) {
        if (isHunTou)
            return getBonusHH(zqItemList, buyType, true);
        else
            return getBonusWithOutHH(zqItemList, buyType);
    }

    private static String getBonusWithOutHH(List<ZqItem> zqItemList, String buyType) {
        //SPF|170329001=3,170329002=1,170329003=0|2*1
        StringBuilder strFinal = new StringBuilder();
        //彩票 种类 -spf
        String cType = "";
        int index = 0;
        for (ZqItem item : zqItemList) {
            //原始数据
            String[] codes = item.getCodes();

            StringBuilder str = new StringBuilder(codes[0]);
            //勾选的数据
            Map<String, Map<String, String>> map = item.getMap();
            for (Map<String, String> mKeys : map.values()) {
                //对应的值
                List<String> cValue = new ArrayList<>();
                //倍数
                List<Float> cMultiple = new ArrayList<>();
                for (String value : mKeys.values()) {
                    //3_cbf_17_500.0_平其它_9:9
                    String[] split = value.split("_");
                    cType = split[1].toUpperCase();
                    cValue.add(split[5]);
                    cMultiple.add(Float.valueOf(split[3]));
                }
                for (int i = 0; i < cValue.size(); i++) {
                    if (i == 0)
                        str.append("=");
                    str.append(cValue.get(i));
                    if (i < cValue.size() - 1)
                        str.append("/");
                }
                str.append("+");
            }
            strFinal.append(str.deleteCharAt(str.length() - 1));
            if (index < zqItemList.size() - 1)
                strFinal.append(",");
            index++;
        }
        strFinal.append("|" + buyType.replaceAll("串", "*").replaceAll("单关","1*1"));
        strFinal.insert(0, cType + "|");
        return strFinal.toString();
    }

    private static String getBonusHH(List<ZqItem> zqItemList, String buyType, boolean isAward) {
        //HH|170503301>RFSF=0/3+DXF=3/0,170504302>RFSF=0/3+DXF=3/0|2*1
        //HH|170508001>SPF=1+RQSPF=1,170508002>SPF=1+RQSPF=1|2*1
        StringBuilder strFinal = new StringBuilder();
        //彩票 种类 -spf
        String cType = "";
        strFinal.append("HH|");
        int index = 0;
        for (ZqItem item : zqItemList) {
            //原始数据
            String[] codes = item.getCodes();

            StringBuilder str = new StringBuilder(codes[0] + ">");
            //勾选的数据
            Map<String, Map<String, String>> map = item.getMap();
            for (Map<String, String> mKeys : map.values()) {
                //对应的值
                List<String> cValue = new ArrayList<>();
                //倍数
                List<Float> cMultiple = new ArrayList<>();
                for (String value : mKeys.values()) {
                    //3_cbf_17_500.0_平其它_9:9
                    String[] split = value.split("_");
                    cType = split[1].toUpperCase();
                    cValue.add(split[5]);
                    cMultiple.add(Float.valueOf(split[3]));
                }
                //这里有时候会出现问题；
                if (cValue != null && !cValue.isEmpty()) {
                    str.append(cType);
                }
                for (int i = 0; i < cValue.size(); i++) {
                    if (i == 0)
                        str.append("=");
                    str.append(cValue.get(i));
                    if (i < cValue.size() - 1)
                        str.append("/");
                }
                str.append("+");
            }
            strFinal.append(str.deleteCharAt(str.length() - 1));
            if (index < zqItemList.size() - 1)
                strFinal.append(",");
            index++;
        }
        //这里表示不是奖金优化
        if (!isAward) {
            strFinal.append("|" + buyType.replaceAll("串", "*").replaceAll("单关","1*1"));
        }
        return strFinal.toString();
    }

    /**
     * @param lists
     * @param perfix 前缀 sfc ssq
     * @return
     */
    public static String getBonusCode(List<Content> lists, String perfix) {
        StringBuilder strFinal = new StringBuilder();
        int index = 0;
        for (Content item : lists) {
            //原始数据
            StringBuilder str = new StringBuilder(perfix + ">");
            //勾选的数据
            Map<String, Map<String, String>> map = item.getMap();

            for (Map<String, String> mKeys : map.values()) {
                //彩票 种类 -spf
                String cType = "";
                //对应的值
                List<String> cValue = new ArrayList<>();
                //倍数
                List<Float> cMultiple = new ArrayList<>();
                for (String value : mKeys.values()) {
                    //3_cbf_17_500.0_平其它_9:9
                    String[] split = value.split("_");
                    cType = split[1].toUpperCase();
                    cValue.add(split[5]);
                    cMultiple.add(Float.valueOf(split[3]));
                }
                str.append(cType);
                for (int i = 0; i < cValue.size(); i++) {
                    if (i == 0)
                        str.append("=");
                    str.append(cValue.get(i));
                    if (i < cValue.size() - 1)
                        str.append("/");
                }
                Float[] floats = cMultiple.toArray(new Float[]{});
                Arrays.sort(floats);
                if (floats.length > 1) {
                    str.append("(" + floats[0] + "~" + floats[floats.length - 1] + ")");
                } else if (floats.length == 1) {
                    str.append("(" + floats[0] + ")");
                }
                str.append("+");
            }
            strFinal.append(str.deleteCharAt(str.length() - 1));
            if (index < lists.size() - 1)
                strFinal.append(",");
            index++;
        }
        return strFinal.toString();
    }

    //使用说明 ：
    //计算奖金
    //计算奖金格式： => codes = 170329001>SPF=3(3.2),170329002>SPF=1(3.2),170329003>SPF=0(3.2)
    //@170127003 表示 胆码
    public static String getBonusCode(List<ZqItem> zqItemList) {
        StringBuilder strFinal = new StringBuilder();
        int index = 0;
        for (ZqItem item : zqItemList) {
            //原始数据
            String[] codes = item.getCodes();

            StringBuilder str = new StringBuilder(codes[0] + ">");
            //勾选的数据
            Map<String, Map<String, String>> map = item.getMap();
            for (Map<String, String> mKeys : map.values()) {
                //彩票 种类 -spf
                String cType = "";
                //对应的值
                List<String> cValue = new ArrayList<>();
                //倍数
                List<Float> cMultiple = new ArrayList<>();
                for (String value : mKeys.values()) {
                    //3_cbf_17_500.0_平其它_9:9
                    String[] split = value.split("_");
                    cType = split[1].toUpperCase();
                    cValue.add(split[5]);
                    cMultiple.add(Float.valueOf(split[3]));
                }
                str.append(cType);
                for (int i = 0; i < cValue.size(); i++) {
                    if (i == 0)
                        str.append("=");
                    str.append(cValue.get(i));
                    if (i < cValue.size() - 1)
                        str.append("/");
                }
                Float[] floats = cMultiple.toArray(new Float[]{});
                Arrays.sort(floats);

                String floatsStr="(";
                for (int i = 0; i < floats.length; i++) {
                    floatsStr+=floats[i]+"~";
                }
                floatsStr=floatsStr.substring(0,floatsStr.length()-1)+")";
                str.append(floatsStr);
              /*  if (floats.length > 1) {
                    str.append("(" + floats[0] + "~" + floats[floats.length - 1] + ")");
                } else if (floats.length == 1) {
                    str.append("(" + floats[0] + ")");
                }*/

                str.append("+");
            }
            strFinal.append(str.deleteCharAt(str.length() - 1));
            if (index < zqItemList.size() - 1)
                strFinal.append(",");
            index++;
        }
        return strFinal.toString();
    }


    public static String formatBank(String bank) {
        StringBuilder stringBuilder=new StringBuilder();

        if (!TextUtils.isEmpty(bank) && bank.length() > 4){

            String[] split = bank.split("");
            int half = split.length / 2;

            for(int i=0;i<split.length;i++){

                if(TextUtils.isEmpty(split[i])){
                    continue;
                }

                if (i<4 || i>=4+half) {
                    stringBuilder.append(split[i]);
                }else{
                    stringBuilder.append("*");
                }
            }
        }else{
            stringBuilder.append(bank);
        }
        return stringBuilder.toString();
    }

    /**
     * 将文件转成base64 字符串
     *
     * @return *
     * @throws Exception
     */

    public static String encodeBase64File(String path) {
        try {
            File file = new File(path);

            FileInputStream inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return new Base64().encode(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 字符串序列化成jsonArray
     *
     * @param content
     * @return
     */
    public static ArrayList<TrendDataBean> string2JsonArray(String content, String mode) {
        ArrayList<TrendDataBean> list = new ArrayList<>();
        try {
            JsonObject jsonObject = new Gson().fromJson(content, JsonObject.class);
            //号码球
            JsonArray alldiffs = jsonObject.getAsJsonArray("opencodes");
            for (int i = alldiffs.size() - 1; i > -1; i--) {
                TrendDataBean trendDataBean = new TrendDataBean();
                JsonObject obj = alldiffs.get(i).getAsJsonObject();
                //预期号码
                JsonObject omission = obj.get("omission").getAsJsonObject();
                JsonArray m0 = omission.get("m0").getAsJsonArray();
                //开出的号码
                String codes = obj.get("codes").getAsString();
                switch (mode) {
                    case "直选一":
                        m0 = omission.get("m1").getAsJsonArray();
                        break;
                    case "直选二":
                        m0 = omission.get("m2").getAsJsonArray();
                        break;
                    case "直选三":
                        m0 = omission.get("m3").getAsJsonArray();
                        break;
                    case "直选四":
                        m0 = omission.get("m4").getAsJsonArray();
                        break;
                    case "直选五":
                        m0 = omission.get("m5").getAsJsonArray();
                        break;
                    case "前零直选":
                        String[] split = codes.split(",");
                        trendDataBean.setOnlySelectTxt(split[0]);
                        break;
                }
                String blue = "";
                List<Integer> awardList = getAwardList(codes, mode);
                for (int j = 0; j < m0.size(); j++) {
                    if (awardList.contains(j)) {
                        blue += "-1";
                    } else {
                        blue += m0.get(j).getAsString();
                    }
                    if (j < m0.size() - 1)
                        blue += ",";
                }
                String term = obj.get("term").getAsString();
                term = term.length() >= 2 ? term.substring(term.length() - 2, term.length()) : term;
                trendDataBean.setBlue(blue);
                trendDataBean.setPid(term + "期");
                trendDataBean.setType("row");
                list.add(trendDataBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //在这里我们重写匹配规则：筛选出中奖的号码；
    public static List<Integer> getAwardList(String codes, String mode) {

        //这里我们是需要根据模式来取出来对应的中奖号码；
        switch (mode) {
            case "前零直选":
            case "直选一":
                return createFirstAwardNumber(codes);
            case "直选二":
                return createSecondAwardNumber(codes);
            case "直选三":
                return createThirdAwardNumber(codes);
            case "前二组选":
                return createTwoStarAwardNumber(codes);
            case "前三组选":
                return createThreeStarAwardNumber(codes);
            default:
                return createDefaultAwardNumber(codes);
        }
    }

    /**
     * 前三直选的条目；
     *
     * @param codes
     * @return
     */
    private static List<Integer> createThirdAwardNumber(String codes) {
        return getIndex(codes, 2);
    }

    /**
     * 前二直选
     *
     * @param codes
     * @return
     */
    private static List<Integer> createSecondAwardNumber(String codes) {
        return getIndex(codes, 1);
    }

    /**
     * 前一直选模式；
     *
     * @param codes
     * @return
     */
    private static List<Integer> createFirstAwardNumber(String codes) {
        return getIndex(codes, 0);
    }

    /**
     * 二星组选的条目；
     *
     * @param codes
     * @return
     */
    private static List<Integer> createTwoStarAwardNumber(String codes) {
        return getList(codes, 2);
    }

    /***
     * 三星组选 item；
     * @return
     */
    private static List<Integer> createThreeStarAwardNumber(String codes) {
        return getList(codes, 3);
    }

    /**
     * 默认是全部都选中；
     *
     * @param codes
     * @return
     */
    private static List<Integer> createDefaultAwardNumber(String codes) {
        return getList(codes, 5);
    }

    /**
     * 取code中的前几位
     *
     * @param codes
     * @param totalIndex
     * @return
     */
    public static List<Integer> getList(String codes, int totalIndex) {

        String[] codeStrArr = codes.split(",");
        List<Integer> indexs = new ArrayList<>();

        if (totalIndex < 0) {
            totalIndex = 0;
        }
        if (totalIndex > codeStrArr.length) {
            totalIndex = codeStrArr.length;
        }
        for (int i = 0; i < totalIndex; i++) {
            indexs.add(Integer.parseInt(codeStrArr[i]) - 1);
        }
        return indexs;
    }


    /**
     * 取code中的第几位
     *
     * @param codes
     * @param index
     * @return
     */
    public static List<Integer> getIndex(String codes, int index) {

        String[] codeStrArr = codes.split(",");
        List<Integer> indexs = new ArrayList<>();

        if (index < 0) {
            index = 0;
        }
        if (index > codeStrArr.length) {
            index = codeStrArr.length;
        }
        indexs.add(Integer.parseInt(codeStrArr[index]) - 1);
        return indexs;
    }



    /**
     * 保留1位小数
     * @param countPrice
     * @return
     */
    public static String decimalFormat(float countPrice){
        DecimalFormat df = new DecimalFormat("#.0");
        return formatDotString(df.format(countPrice));
    }

    /**
     * 保留2位小数
     * @param countPrice
     * @return
     */
    public static String decimalFormatTwo(float countPrice){
        DecimalFormat df = new DecimalFormat("#.00");
        String format = df.format(countPrice);
        //上面有一个问题就是如果是0.4会变成.4
        return formatDotString(format);
    }

    /**
     * 将.4 转换为0.4；
     * @param targetStr
     * @return
     */
    public static String formatDotString(String targetStr){
        String substring = targetStr.substring(0, 1);
        if(".".equals(substring)) {
            targetStr="0"+targetStr;
        }
        return targetStr;
    }
}
